//Copyright (c) Microsoft Corporation.  All rights reserved.

using System;
using System.Collections.Generic;
using System.Text;
using Eas.WebCrawler.Model.Actions;
using Eas.WebCrawler.Model.Events;
using Eas.WebCrawler.Model.StateTrackers;

namespace Eas.WebCrawler.Model.Core {
    internal class WorkerContext : Eas.WebCrawler.Common.BaseDisposable {
        /// <summary>The <see cref="ActionFactory"/> used to create new <see cref="Actions.ProcessingAction"/>s.</summary>
        /// <remarks>Backing field for the ActionFactory property.</remarks>
        /// <seealso cref="ActionFactory"/>
        private ActionFactory _ActionFactory;

        /// <summary>The <see cref="Eas.WebCrawler.Interfaces.WebCrawlerContext"/> to use when processing.</summary>
        /// <remarks>Backing field for the WebCrawlerContext property.</remarks>
        /// <seealso cref="WebCrawlerContext"/>
        private Eas.WebCrawler.Interfaces.WebCrawlerContext _WebCrawlerContext;

        /// <summary>The <see cref="Eas.WebCrawler.Interfaces.WebCrawlerEvents"/> used to handle events while processing.</summary>
        /// <remarks>Backing field for the Events property.</remarks>
        /// <seealso cref="Events"/>
        private Eas.WebCrawler.Interfaces.WebCrawlerEvents _Events;

        /// <summary>The <see cref="Eas.WebCrawler.Model.Gateway.WebGateway"/> to use while processing.</summary>
        /// <remarks>Backing field for the WebGateway property.</remarks>
        /// <seealso cref="WebGateway"/>
        private Eas.WebCrawler.Model.Gateway.WebGateway _WebGateway;

        /// <summary>The <see cref="StateTracker"/> to use while processing.</summary>
        /// <remarks>Backing field for the StateTracker property.</remarks>
        /// <seealso cref="StateTracker"/>
        private StateTracker _StateTracker;

        /// <summary>The <see cref="Eas.WebCrawler.Model.Events.EventReporter"/> to use while processing.</summary>
        /// <remarks>Backing field for the EventReporter property.</remarks>
        /// <seealso cref="EventReporter"/>
        private EventReporter _EventReporter;

        public WorkerContext(Eas.WebCrawler.Interfaces.WebCrawlerContext webCrawlerContext, ActionFactory actionFactory, Eas.WebCrawler.Interfaces.WebCrawlerEvents events, Eas.WebCrawler.Model.Gateway.WebGateway webGateway, EventReporter eventReporter, StateTracker stateTracker) {
            _ActionFactory = actionFactory;
            _WebCrawlerContext = webCrawlerContext;
            _Events = events;
            _WebGateway = webGateway;
            _EventReporter = eventReporter;
            _StateTracker = stateTracker;

            RegisterComponents(_EventReporter, _ActionFactory, _StateTracker);

        }

        /// <summary>The <see cref="ActionFactory"/> used to create new <see cref="Actions.ProcessingAction"/>s.</summary>
        public ActionFactory ActionFactory {
            get { return _ActionFactory; }
        }

        /// <summary>The <see cref="Eas.WebCrawler.Interfaces.WebCrawlerContext"/> to use when processing.</summary>
        public Eas.WebCrawler.Interfaces.WebCrawlerContext WebCrawlerContext {
            get { return _WebCrawlerContext; }
        }

        /// <summary>The <see cref="Eas.WebCrawler.Interfaces.WebCrawlerEvents"/> used to handle events while processing.</summary>
        public Eas.WebCrawler.Interfaces.WebCrawlerEvents Events { 
            get { return _Events; }
        }

        /// <summary>The <see cref="Eas.WebCrawler.Model.Gateway.WebGateway"/> to use while processing.</summary>
        public Eas.WebCrawler.Model.Gateway.WebGateway WebGateway {
            get { return _WebGateway; }
        }

        /// <summary>The <see cref="Eas.WebCrawler.Model.Events.EventReporter"/> to use while processing.</summary>
        public EventReporter EventReporter {
            get { return _EventReporter; }
        }

        /// <summary>The <see cref="StateTracker"/> to use while processing.</summary>
        public StateTracker StateTracker {
            get { return _StateTracker; }
        }

        protected override void Dispose(bool isDisposing) {
            try {
                _WebCrawlerContext.Dispose();
            }
            catch (System.Exception) { }
            try {
                _StateTracker.Dispose();
            }
            finally {
                base.Dispose(isDisposing);
            }
        }

        private void RegisterComponents(params WorkerComponent[] components) {
            foreach (WorkerComponent component in components) {
                component.Initialize(this);
            }
        }
    }
}
